<?php
declare (strict_types = 1);

namespace app\controller\backend\course;

use app\model\communal\CourseModel;
use app\service\logic\common\course\CourseService;
use app\validate\common\course\CourseValidate;
use think\App;
use think\Request;
use think\Response;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class CourseController extends BackendController
{
    protected $service;

    public function __construct(App $app, CourseService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 视图
     *
     * @return string
     */
    public function view(): string
    {
        return $this->layout('backend/course/content', 'course_content');
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $this->validate($params, CourseValidate::class . '.index');

        $fun = function ($model) {
            // 类型 1 直播课程 2 点播课程
            $typeMap = [1 => '直播课程', 2 => '点播课程'];
            $model['type_map'] = $typeMap[$model['type']];
            unset($model['belongCategory'], $model['byCreator']);
            return $model;
        };
        $result = $this->service->getList($params, $search_fields, $search, ['belongCategory', 'byCreator'], $fun);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options(): Json
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $drop = $this->service->getOptions($params, $search_fields, $search);
        return amis(['options' => $drop]);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request): Response
    {
        $params = $request->only($this->service->getSaveColumn());
        $params['admin_id'] = $request->middleware('admin')['id'];
        $this->validate($params, CourseValidate::class . '.save');

        $result = $this->service->create($params);

        return amis($result);
    }

    /**
     * 显示资源
     *
     * @param $id
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read($id): Json
    {
        $result = $this->service->getSimpleDetail($id);
        return amis($result);
    }

    /**
     * 保存更新的资源
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function update(Request $request): Json
    {
        $params = $request->only($this->service->getUpdateColumn());
        $this->validate($params, CourseValidate::class . '.update');

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * 单项修改
     *
     * @param Request $request
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function item(Request $request): Json
    {
        $params = $request->only($this->service->getItemColumn());
        $this->validate($params, CourseValidate::class . '.item');

        $result = $this->service->item($params);
        return amis($result);
    }

    /**
     * 课程类型选项
     *
     * @return Json
     */
    public function type()
    {
        $options = $this->service->amisOptions(CourseModel::$typeOptions);
        return amis(['options' => $options]);
    }
}
